#!/usr/bin/env python

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)

def PrintLinks():
    for l in br.links():
     	print l

def GetLinks():
    linksList = []
    for l in br.links():
        linksList.append(l)
    return linksList

def FindLastClosingSograim(func):
    pos = 0
    found_pos = 0
    for c in func:
	if c == ")":
	    found_pos = pos
	pos +=1
    return found_pos      

def HandleSubPage(link):
    page = br.open(link)
    htmlText = page.read()
    func_sig_start_index = htmlText.find("PFN")
    func_sig_end_index = htmlText.find("{",func_sig_start_index)-1
#    func_sig_end_index = FindLastClosingSograim(htmlText)
    signature_text = htmlText[func_sig_start_index:func_sig_end_index+1]
    return signature_text

def GetFunctionSignatures(main_url,first_func,last_func):
    result_funcs = {}
    start_url = main_url
    br.open(start_url)
    print 'Opened "%s"' % start_url
    assert br.viewing_html()                 
  
    
    can_start = False
    main_page_links = GetLinks()
    
    for l in main_page_links:
        if l.text == first_func:
            can_start = True    
        if can_start == False:
            continue
	
#	if not 'CalcPrivateShaderSize' in l.text:
#	    continue   
        print 'Processing "%s"...' % l.text ,

#	if l.text == 'CalcPrivateShaderSize':
#	    print "found"
	
        func_sig_text = HandleSubPage(l.url)
        char_val = chr(0xa0)
        result_funcs[l.text] = func_sig_text.replace('\xc2',' ').replace('\xa0',' ')
#	result_funcs.append(func_sig_text.replace('\xc2',' ').replace('\xa0',' '))
        print 'done.'
        if l.text == last_func:
            can_start = False    
    result_funcs_list = []
    for k,d in result_funcs.items():
	result_funcs_list.append(d)
    return result_funcs_list
 
  


